package com.kd.eb.service;

import com.kd.eb.mapper.EbProductMapper;
import com.kd.eb.model.EbProduct;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.HashSet;
import java.util.Set;

@Service
public class ProductService {
    @Resource
    private EbProductMapper ebProductMapper;

    public EbProduct detail(int pid){
        EbProduct ebProduct = ebProductMapper.selectById(pid);
        return ebProduct;
    }

    public void addCar(int epId, HttpServletRequest request){
        EbProduct carProduct = ebProductMapper.selectById(epId);
        HttpSession session = request.getSession();
        Set<EbProduct> set = null;
        set = (Set<EbProduct>)session.getAttribute("car");
        if(set==null){
            set = new HashSet<>();
        }
        boolean flag = false;
        for(EbProduct oldEbProduct:set){
            if(carProduct.getEpId().equals(oldEbProduct.getEpId())){
                oldEbProduct.setCount(oldEbProduct.getCount()+1);
                flag = true;
                break;
            }
        }
        if(!flag){
            set.add(carProduct);
        }
        session.setAttribute("car",set);
    }

}
